package ch02.impl;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @auth 邝志豪
 * @date 2024/1/5 14:13
 */
public class Ac2Impl {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db=dbf.newDocumentBuilder();
        Document doc= db.parse("test/ch02/acXml/ac2.xml");
        //创建Brand节点
        Element brandElement= doc.createElement("Brand");
        brandElement.setAttribute("name","三星");
        //创建Type节点
        Element typeElement=doc.createElement("Type");
        typeElement.setAttribute("name","NoteX");
        //添加父子关系
        brandElement.appendChild(typeElement);
        Element phoneElement = (Element) doc.getElementsByTagName("PhoneInfo").item(0);
        phoneElement.appendChild(brandElement);
        //保存XML文件
        TransformerFactory transformerFactory =TransformerFactory.newInstance();
        Transformer transformer=transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(doc);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        StreamResult result=new StreamResult(new FileOutputStream("test/ch02/acXml/ac2.xml"));
        //把DOM树转换为XML文件
        transformer.transform(domSource,result);
    }
    @Test
    public void textUpdated() throws ParserConfigurationException, IOException, SAXException, TransformerException {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db=dbf.newDocumentBuilder();
        Document doc= db.parse("test/ch02/acXml/ac2.xml");
        NodeList list=doc.getElementsByTagName("Brand");
        for(int i=0; i<list.getLength();i++){
            Element brandElement=(Element)list.item(i);
            String brandName=brandElement.getAttribute("name");
            if(brandName.equals("三星")){
                brandElement.setAttribute("name","SAMSUNG");   //修改属性
            }
        }
        TransformerFactory transformerFactory=TransformerFactory.newInstance();
        Transformer transformer=transformerFactory.newTransformer();
        DOMSource domSource=new DOMSource(doc);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING,"utf-8");
        StreamResult result=new StreamResult(new FileOutputStream("test/ch02/acXml/ac2.xml"));
        //把DOM树转换为XML文件
        transformer.transform(domSource,result);
    }
    @Test
    public void testDelete() throws TransformerException, IOException, ParserConfigurationException, SAXException {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db=dbf.newDocumentBuilder();
        Document doc= db.parse("test/ch02/acXml/ac2.xml");
        NodeList list=doc.getElementsByTagName("Brand");
        for (int i=0; i<list.getLength(); i++){
            Element brandElement=(Element) list.item(i);
            String brandName=brandElement.getAttribute("name");
            if(brandName.equals("SAMSUNG")){
                brandElement.getParentNode().removeChild(brandElement);
            }
        }

        TransformerFactory transformerFactory=TransformerFactory.newInstance();
        Transformer transformer=transformerFactory.newTransformer();
        DOMSource domSource=new DOMSource(doc);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING,"utf-8");
        StreamResult result=new StreamResult(new FileOutputStream("test/ch02/acXml/ac2.xml"));
        //把DOM树转换为XML文件
        transformer.transform(domSource,result);
    }
}
